/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Agency.models;

import Agency.views.AvailableHouses;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.util.ArrayList;

/**
 *
 * @author wangari
 */
public class AvailableHousesController extends javax.swing.JFrame {
Connection conn = null;
private ActionListener actionlistener;
public AvailableHouses allHouses;
public HouseModel  theHouse;
    ArrayList<HouseModel> theList = new ArrayList<>();

/**
 * Constructor  that instantiates an object  of the available houses
 */
public AvailableHousesController(){
   allHouses= new  AvailableHouses();
  // allHouses.setVisible(true);
}
 /**
  *Getting all the apartments available from all the different towns
  */  
public void getTheApartments(){
               try {
 Class.forName("com.mysql.jdbc.Driver").newInstance();
 conn = java.sql.DriverManager.getConnection(
 "jdbc:mysql://localhost/MusyiAgency?user=root&password=");
  System.out.println("its connected");

   java.sql.Statement s = conn.createStatement();
   java.sql.ResultSet  r = s.executeQuery("SELECT HouseType,TownLocation,HouseRent,HousePlotNumber FROM AllHouses WHERE HouseType like 'Apartment'");

  while(r.next()) {
  allHouses.getTheList().addElement( "House type: "+ r.getString("HouseType")+"  " + "Location: " +
    r.getString("TownLocation")+"   "+ "Rent: " +r.getString("HouseRent")+"   "+ "Plot Number: " +
          r.getString("HousePlotNumber") );

  }
  
 }
 catch (Exception e) {
  System.out.println(e);
  System.exit(0);
 }
}

/**
 * Getting all the 3bedroom houses available from all the towns
 */

public void get3bedrooms(){
     try {
 Class.forName("com.mysql.jdbc.Driver").newInstance();
 conn = java.sql.DriverManager.getConnection(
 "jdbc:mysql://localhost/MusyiAgency?user=root&password=");
  System.out.println("its connected");

   java.sql.Statement s = conn.createStatement();
   java.sql.ResultSet  r = s.executeQuery("SELECT HouseType,TownLocation,HouseRent,HousePlotNumber FROM AllHouses WHERE HouseType like '3Bedroom'");

  while(r.next()) {
  allHouses.getTheList().addElement( "House type: "+ r.getString("HouseType")+"  " + "Location: " +
    r.getString("TownLocation")+"   "+ "Rent: " +r.getString("HouseRent")+"   "+ "Plot Number: " +
          r.getString("HousePlotNumber") );

  }
  
 }
 catch (Exception e) {
  System.out.println(e);
  System.exit(0);
 }
    
}
/**
 * Getting all the availABLE 2 bedroom houses vacant in all the different towns
 */
public void get2bedrooms(){
     try {
 Class.forName("com.mysql.jdbc.Driver").newInstance();
 conn = java.sql.DriverManager.getConnection(
 "jdbc:mysql://localhost/MusyiAgency?user=root&password=");
  System.out.println("its connected");

   java.sql.Statement s = conn.createStatement();
   java.sql.ResultSet  r = s.executeQuery("SELECT HouseType,TownLocation,HouseRent,HousePlotNumber FROM AllHouses WHERE HouseType like '2Bedroom'");

  while(r.next()) {
  allHouses.getTheList().addElement( "House type: "+ r.getString("HouseType")+"  " + "Location: " +
    r.getString("TownLocation")+"   "+ "Rent: " +r.getString("HouseRent")+"   "+ "Plot Number: " +
          r.getString("HousePlotNumber") );

  }
  
 }
 catch (Exception e) {
  System.out.println(e);
  System.exit(0);
 }
    
}
/**
 * Getting all the available  1 bedroom houses from all the different towns
 */
public void get1bedrooms(){
     try {
 Class.forName("com.mysql.jdbc.Driver").newInstance();
 conn = java.sql.DriverManager.getConnection(
 "jdbc:mysql://localhost/MusyiAgency?user=root&password=");
  System.out.println("its connected");

   java.sql.Statement s = conn.createStatement();
   java.sql.ResultSet  r = s.executeQuery("SELECT HouseType,TownLocation,HouseRent,HousePlotNumber FROM AllHouses WHERE HouseType like '1Bedroom'");

  while(r.next()) {
  allHouses.getTheList().addElement( "House type: "+ r.getString("HouseType")+"  " + "Location: " +
    r.getString("TownLocation")+"   "+ "Rent: " +r.getString("HouseRent")+"   "+ "Plot Number: " +
          r.getString("HousePlotNumber") );

  }
  
 }
 catch (Exception e) {
  System.out.println(e);
  System.exit(0);
 }
    
}
/**
 * Displaying all the available bedsitter in all the towns
 */
public void getBedsitters(){
     try {
 Class.forName("com.mysql.jdbc.Driver").newInstance();
 conn = java.sql.DriverManager.getConnection(
 "jdbc:mysql://localhost/MusyiAgency?user=root&password=");
  System.out.println("its connected");

   java.sql.Statement s = conn.createStatement();
   java.sql.ResultSet  r = s.executeQuery("SELECT HouseType,TownLocation,HouseRent,HousePlotNumber FROM AllHouses WHERE HouseType like 'BedSitter'");

  while(r.next()) {
  allHouses.getTheList().addElement( "House type: "+ r.getString("HouseType")+"  " + "Location: " +
    r.getString("TownLocation")+"   "+ "Rent: " +r.getString("HouseRent")+"   "+ "Plot Number: " +
          r.getString("HousePlotNumber") );

  }
  
 }
 catch (Exception e) {
  System.out.println(e);
  System.exit(0);
 }
    
}
/**
 * Displaying all the single rooms in all the towns in a list
 */
public void getSingleRooms(){
     try {
 Class.forName("com.mysql.jdbc.Driver").newInstance();
 conn = java.sql.DriverManager.getConnection(
 "jdbc:mysql://localhost/MusyiAgency?user=root&password=");
  System.out.println("its connected");

   java.sql.Statement s = conn.createStatement();
   java.sql.ResultSet  r = s.executeQuery("SELECT HouseType,TownLocation,HouseRent,HousePlotNumber FROM AllHouses WHERE HouseType like 'SingleRoom'");

  while(r.next()) {
  allHouses.getTheList().addElement( "House type: "+ r.getString("HouseType")+"  " + "Location: " +
    r.getString("TownLocation")+"   "+ "Rent: " +r.getString("HouseRent")+"   "+ "Plot Number: " +
          r.getString("HousePlotNumber") );

  }
  
 }
 catch (Exception e) {
  System.out.println(e);
  System.exit(0);
 }
    
}
/**
 * closing the view
 */
public void closeThePage(){
  allHouses.setVisible(false);  
}

}
